(function(angular){
	angular.module('starter.controllers', [])
	.controller('BaseCtrl', ['$scope', function ($scope) {
		$scope.imgUrl=server.imgUrl;
		$scope.slides=$scope.infolist[0];
		console.log($scope.slides);
        $scope.getNews = function (n) {
            // 安卓平台不会自动触发加载
            if (ionic.Platform.isAndroid()) {
                n.doRefresh();
            }
            // 初始化数据，和回调函数
            n.callback = function () {
                $scope.$broadcast('scroll.refreshComplete');
                $scope.$broadcast('scroll.infiniteScrollComplete');
            }
        }
        $scope.getNews($scope.slides);
	}])

	.controller('tab1Ctrl', ['$scope','Tab1Service','$timeout','BaseService','$controller','$state',function ($scope,Tab1Service,$timeout,BaseService,$controller,$state) {
		$scope.infolist=Tab1Service.getInfo();
		$controller('BaseCtrl',{$scope:$scope});
		$scope.goDetail=function(item){
			$state.go('tab.tab1-detail',{id:item.id,title:item.name})
		}
	}])

	.controller('Tab1DetailCtrl', function ($scope, $stateParams, $timeout, $ionicLoading,Tab1Service) {
        var id = $stateParams.id;
        var title=$stateParams.title;
        $scope.title=title;
        $scope.imgUrl=server.imgUrl;
        Tab1Service.getDetails(id).success(function (response) {
            $scope.item = response;
        });

    })


	.controller('tab2Ctrl',function($scope,Tab2Service,BaseService,$controller,$state){
		$scope.infolist=Tab2Service.getInfo();
		$controller('BaseCtrl',{$scope:$scope});
		$scope.goDetail=function(item){
			$state.go('tab.tab2-detail',{id:item.id,title:item.name})
		}
	})

	.controller('Tab2DetailCtrl', function ($scope, $stateParams, $timeout, $ionicLoading,Tab2Service) {
        var id = $stateParams.id;
        var title=$stateParams.title;
        $scope.title=title;
        $scope.imgUrl=server.imgUrl;
        Tab2Service.getDetails(id).success(function (response) {
            $scope.item = response;
            console.log($scope.item);
        });

    })

	.controller('tab3Ctrl', function($scope,$state,$ionicModal,$rootScope) {
		$rootScope.loginT='tab.tablogin';
		if(window.localStorage['loginNow']){
			$rootScope.loginT='tab.tabsucess';
			$rootScope.Lname=window.localStorage['name'];
			console.log($rootScope.Lname);
		}
		$scope.goLogin=function(){
			$state.go($rootScope.loginT);
		}
		$ionicModal.fromTemplateUrl('templates/self.html', {
    	    scope: $scope,
	    animation: 'slide-in-up'
	 	 }).then(function(modal) {
	       $scope.modal = modal;
	    });
        $scope.openModal = function () {
            $scope.modal.show();
        };
        $scope.closeModal = function () {
            $scope.modal.hide();
        };
	})

	.controller('TabLoginCtrl', function($scope,$timeout,LoginService,$state,$rootScope,$ionicTabsDelegate, $ionicSlideBoxDelegate,$ionicLoading) {

		/*选项卡*/
		var loginTab=$ionicTabsDelegate.$getByHandle('loginTab');
		var loginSlide=$ionicSlideBoxDelegate.$getByHandle('loginSlide');

		$scope.slideMove=function(){
			loginTab.select(loginSlide.currentIndex());
		}
		$scope.loginSelectedTab=function(index){
            loginSlide.slide(index);
		}
		/*登录*/
        $scope.getLogin = {
            name: '',
            password:''
        };
       $scope.getRegister = {
       		email:'',
            name: '',
            password:''
        };
		$scope.login=function(){
			LoginService.SLogin($scope.getLogin.name,$scope.getLogin.password).success(function (response) {
				if(response.status){

					$ionicLoading.show({
			        	template: '登录中...'
				    });

					$timeout(function(){
						$ionicLoading.hide();
						window.localStorage['loginNow']=response.access_token;
						window.localStorage['name']=$scope.getLogin.name;
						$rootScope.Lname=$scope.getLogin.name;
						$rootScope.loginT='tab.tabsucess';
						$state.go('tab.tab3');
					},1200)
					
				}else{
					alert(response.msg)
				}
			});
		}
		/*注册*/
		$scope.register=function(){
			LoginService.Sregister($scope.getRegister.name,$scope.getRegister.email,$scope.getRegister.password).success(function (response) {
				if(response.status){
					$ionicLoading.show({
			        	template: '注册成功'
				    });

				    $timeout(function(){
				    	$ionicLoading.hide();
						loginTab.select(0);
		            	loginSlide.slide(0);
				    },1000)
		
				}else{
					alert(response.msg)
				}
        	});
		}
		/*加载动作*/
	})
	.controller('TabScCtrl', function($scope) {
	})
	.controller('TabsucessCtrl', function($scope,$timeout,TabsucessService,$state,$rootScope,$ionicLoading) {
		TabsucessService.getUser(window.localStorage['loginNow']).success(function(response){
			$scope.user=response;
		})
		$scope.logout=function(){
			$ionicLoading.show({
				template:'正在注销，请稍等...'
			})
		    $timeout(function(){
		    	$ionicLoading.hide();
				/*注销登录令牌*/
				window.localStorage.removeItem('loginNow');
				$rootScope.loginT='tab.tablogin';
				$rootScope.Lname=null;
				$state.go('tab.tab3');
		    },1000)
		
		}
	})

})(angular);

